#include <iostream>
#include <vector>
#include <queue>
#define PII pair<int, int>
using namespace std;

int prim(vector<vector<PII>> adj) {
    int min_len = 0;
    int m = adj.size();
    vector<bool> visited(m, false);
    priority_queue<PII, vector<PII>, greater<PII>> pq;
    pq.push({0, 0});
    // visited[0] = true;

    while (!pq.empty()) {
        auto cur = pq.top();
        pq.pop();
        
        if (visited[cur.second]) {
            continue;
        }
        
        visited[cur.second] = true;
        min_len += cur.first;

        for (auto n : adj[cur.second]) {
            if (!visited[n.second]) {
                // visited[n.second] = true;
                pq.push(n);
            }
        }
    }
    return min_len;
}

int main() {
    vector<vector<PII>> adj = {
        {{2, 1}, {4, 2}, {7, 3}},
        {{2, 0}, {1, 2}, {2, 4}},
        {{1, 1}, {1, 3}, {4, 0}, {6, 4}},
        {{1, 2}, {7, 0}}, 
        {{2, 1}, {6, 2}}
    };

    cout << prim(adj) << endl;
    return 0;
}